/**
 * e-fuli.com Inc.
 * Copyright (c) 2015-2018 All Rights Reserved.
 */
package ltd.newbee.mall.util;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <pre>
 * 字符串工具类
 * </pre>
 * @author jimmy
 * @version $Id: StringUtil.java, v 0.1 2017年12月24日 下午10:44:13 jimmy Exp $
 */
public final class StringUtil {

    public static final String COMMA = ",";

    private StringUtil() {

    }

    /**
     * <pre>
     * 字符串转long(如果不能转为long，返回null，不抛出异常)
     * </pre>
     * @param str
     * @return
     */
    public static Long safeStrToLong(final String str) {
        try {
            return Long.parseLong(str);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * <pre>
     * 字符串转double(如果不能转为double，返回null，不抛出异常)
     * </pre>
     * @param str
     * @return
     */
    public static Double safeStrToDouble(final String str) {
        try {
            return Double.parseDouble(str);
        } catch (Exception e) {
            return null;
        }
    }

    /** 下划线转驼峰 */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Pattern linePattern = Pattern.compile("_(\\w)");
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /** 驼峰转下划线 */
    public static String humpToLine(String str) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static boolean isEmpty(String str) {
        return str == null || str.equals("");
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static String join(Collection<String> list, String separator) {
        StringBuilder buffer = new StringBuilder();
        int i = 0;
        for (String str : list) {
            if (i > 0) {
                buffer.append(separator);
            }
            buffer.append(str);
            i++;
        }
        return buffer.toString();
    }

    public static String join(String[] arr, String separator) {
        StringBuilder buffer = new StringBuilder();
        int i = 0;
        for (String str : arr) {
            if (i > 0) {
                buffer.append(separator);
            }
            buffer.append(str);
            i++;
        }
        return buffer.toString();
    }

    public static String sqlIn(final String[] arr) {
        final StringBuilder sb = new StringBuilder();
        sb.append("('");
        sb.append(arr[0]);
        sb.append("'");
        for (int i = 1; i < arr.length; i++) {
            sb.append(",'");
            sb.append(arr[i]);
            sb.append("'");
        }
        sb.append(")");
        return sb.toString();
    }

    public static String removeBlank(String str) {
        return str.replaceAll("\\s*", "");
    }

    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static boolean isBlank(String str) {
        if (str == null || str.length() == 0) {
            return true;
        }
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static String trim(String str) {
        if (isBlank(str)) {
            return str;
        }
        return str.trim();
    }

    /**
     * <pre>
     * split，注意：对所有列表项取trim后的结果
     * </pre>
     * @param str
     * @return
     */
    public static String[] split(String str) {
        return split(str, COMMA);
    }

    /**
     * <pre>
     * split，注意：对所有列表项取trim后的结果
     * </pre>
     * @param str
     * @param separator
     * @return
     */
    public static String[] split(String str, String separator) {
        if (isBlank(str)) {
            return new String[] {};
        }
        String[] arr = str.split(separator);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i].trim();
        }
        return arr;
    }

    public static Set<Character> initCheckerSet(String checker) {
        final Set<Character> s = new HashSet<>();
        if (StringUtil.isBlank(checker)) {
            return s;
        }
        final char[] arr = checker.toCharArray();
        for (char c : arr) {
            s.add(c);
        }
        return s;
    }

    /**
     * <pre>
     * 逐个字符校验
     * </pre>
     * @param toCheck
     * @param checker
     * @return 校验成功true校验失败false
     */
    public static boolean filterCharByChar(String toCheck, Set<Character> checker) {
        final int length = toCheck.length();
        for (int i = 0; i < length; i++) {
            if (!checker.contains(toCheck.charAt(i))) {
                return false;
            }
        }
        return true;
    }

}
